package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.client.VodClient;
import com.atguigu.serviceedu.entity.EduVideo;
import com.atguigu.serviceedu.mapper.EduVideoMapper;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author jyd
 * @since 2022-03-20
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {
        //根据课程id查询课程所有小节的视频id
        //QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        //查询课程id下所有小节
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        //创建一个视频id集合
        List<String> videoIdList = new ArrayList();
        //遍历查询出来的结果 得到视频id
        for (EduVideo eduVideo : eduVideos) {
            String videoSourceId = eduVideo.getVideoSourceId();
            //添加到视频id集合中
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIdList.add(videoSourceId);
            }
        }
        //删除多个视频
        if(!videoIdList.isEmpty()){
        //if(videoIdList.size() > 0){
            vodClient.removeVideoList(videoIdList);
        }
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
